/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbcgateway.smscbresponse;

import com.elcom.emocbc.dbmapping.tables.pojos.Bsc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author GiangLT
 */
public class GetBscsResponseParser {

    private static final Logger logger = LoggerFactory.getLogger(GetBscsResponseParser.class);

    public static List<Bsc> parse(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilder dBuilder = null;
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dBuilder = dbFactory.newDocumentBuilder();

            Document doc = dBuilder.parse(inputStream);
            doc.getDocumentElement().normalize();

//        Element cbcResponseNode = (Element) doc.getElementsByTagName("cbcResponse").item(0);

            NodeList bscNodes = doc.getElementsByTagName("bsc");
            List<Bsc> bscs = new ArrayList<Bsc>(bscNodes.getLength());
            for (int i = 0; i < bscNodes.getLength(); i++) {
                Element bscNode = (Element) bscNodes.item(i);

                Bsc bsc = new Bsc();
                Element propertiesNode = (Element) bscNode.getElementsByTagName("properties").item(0);
                NodeList propNodes = propertiesNode.getElementsByTagName("property");

                for (int j = 0; j < propNodes.getLength(); j++) {
                    Element propertyNode = (Element) propNodes.item(j);
                    String propertyName = propertyNode.getAttribute("name");
                    String value = propertyNode.getAttribute("value");
                    if (propertyName.equalsIgnoreCase("name")) {
                        bsc.setBscName(value);
                    } else if (propertyName.equalsIgnoreCase("bscid")) {
                        bsc.setBscId(value);
                    } else if (propertyName.equalsIgnoreCase("version")) {
                        bsc.setVersion(value);
                    } else if (propertyName.equalsIgnoreCase("primaryport")) {
                        bsc.setPrimaryPort(value);
                    } else if (propertyName.equalsIgnoreCase("vendor")) {
                        bsc.setVendor(value);
                    } else if (propertyName.equalsIgnoreCase("bscprimaryaddress")) {
                        bsc.setPrimaryAddress(value);
                    } else if (propertyName.equalsIgnoreCase("secondaryport")) {
//                    bsc.set(value);
                    } else if (propertyName.equalsIgnoreCase("cbcprimaryaddress")) {
//                    bsc.setBscName(value);
                    } else if (propertyName.equalsIgnoreCase("protocol")) {
                        bsc.setProtocol(value);
                    }
                }

                Element statusNode = (Element) bscNode.getElementsByTagName("status").item(0);
                String status = statusNode.getElementsByTagName("status").item(0).getTextContent();
                String description = statusNode.getElementsByTagName("description").item(0).getTextContent();
                String initializationTime = statusNode.getElementsByTagName("initializationTime").item(0).getTextContent();
                String reconnectionNumber = statusNode.getElementsByTagName("reconnectionNumber").item(0).getTextContent();

                bsc.setStatus(status);
                bsc.setDescription(description);
                try {
                    Date initTime = new SimpleDateFormat(Bsc.INIT_TIME_DATE_FORMAT).parse(initializationTime);
                    bsc.setInitTime(new Timestamp(initTime.getTime()));
                } catch (ParseException ex) {
//                logger.error("", ex);
                }

                try {
                    bsc.setReconnectionNum(Integer.parseInt(reconnectionNumber));
                } catch (NumberFormatException ex) {
                    logger.debug("No reconnectionNumber defined", ex);
                }

                bscs.add(bsc);
            }

            return bscs;
        } finally {
            if (dBuilder != null) {
                dBuilder.reset();
            }
            IOUtils.closeQuietly(inputStream);
        }
    }
}
